define("vw.header", ['vw.dialog', 'bootstrap', 'css!../lib/vw/css/vw.header.css'], function(vw_dialog) {
	"use strict";
	var $header, $footer;
	var _uid, _fid, _fnName, _uName, _prjName, _uCode, _mainform, _uEmail;
	var msgcontents = [];

	function Init(headerID, footerID) {
		_uid = vw_static.getCookie(window.location.host);
		_fid = vw_static.getQueryString(window, "fid");
		var prjInfo = vw_static.getCookie(_uid);
		var fn = vw_static.getQueryString(window, "fn");

		if(_uid == null || _fid == null || prjInfo == null) {
			window.location.href = VW_INTERFACE.PAGES.LOGIN;
		}

		window.ajaxCount = 0; //用于判断loading是否消失在，vw.common里实现

		if(fn) {
			_fnName = vw_static.uncompileStr(fn);
			$(document).attr('title', _fnName);
		} else {
			_fnName = "";
		}

		var info = prjInfo.split('|');
		_uName = info[0];
		_prjName = info[1];
		_uCode = info[2];
		_mainform = info[3];
		_uEmail = info[4];

		headerID = headerID ? headerID : "vw-header";
		footerID = footerID ? footerID : "vw-footer"
		$header = $("#" + headerID);
		$footer = $("#" + footerID);

		createHeader();

		$("[data-toggle='tooltip']").tooltip();

		history.pushState(null, null, document.URL);
		window.addEventListener('popstate', function() {
			history.pushState(null, null, document.URL);
		});

		refreshMsg();

		$('#_userinfo').click(function() {
			vw_dialog().open({
				title: '用户信息',
				fnButtonName: "保存",
				paraCatalogID: "userID",
				catalogID: _uid,
				infourl: VW_INTERFACE.WEBSERVICE.LoginManage("GetUserInfo"),
				saveurl: VW_INTERFACE.WEBSERVICE.LoginManage("SaveUserInfo")
			});
		});

		$('#_inbox').click(function() {
			showInbox();
		});

		$('#msg_0').click(function() {
			ShowOneMsg(0);
		});

		$('#msg_1').click(function() {
			ShowOneMsg(1);
		});

		$('#msg_2').click(function() {
			ShowOneMsg(2);
		});

		$('#msg_all').click(function() {
			showInbox();
		});

		function showInbox() {
			bDialog.open({
				title: '站内信',
				url: 'frame.inbox.html',
				dialogMaxButton: false,
				customClass: 'blue',
				width: 850,
				height: 500,
				onHidden: refreshMsg,
			});
		}

		$('#_changepwd').click(function() {
			bDialog.open({
				title: '修改密码',
				url: 'frame.changepwd.html',
				dialogMaxButton: false,
				customClass: 'blue',
				width: 600,
				height: 260,
				fnButton: true,
				fnButtonName: "保存",
				fn: function() {
					$('.bDialogBodyFrame')[0].contentWindow.save();
				},
			});
		});

		$('#_backtomain').click(function() {
			vw_common.ConLog(_uName, "exitFn|" + _fnName);
			window.location.href = _mainform;
		});

		$('#_exit').click(function() {
			vw_static.delCookie(window.location.host);
			vw_static.delCookie(_uid);

			vw_common.ConLog(_uName, "exitFn|" + _fnName);
			vw_common.ConLog(_uName, "logout");

			window.location.href = VW_INTERFACE.PAGES.LOGIN;
		});

		unloadListener();
	}

	function unloadListener() {
		var userAgent = navigator.userAgent; //取得浏览器的userAgent字符串    
		var isOpera = userAgent.indexOf("Opera") > -1; //判断是否Opera浏览器    
		var isIE = userAgent.indexOf("compatible") > -1 && userAgent.indexOf("MSIE") > -1 && !isOpera; //判断是否IE浏览器  
		var isIE11 = userAgent.indexOf("rv:11.0") > -1; //判断是否是IE11浏览器  
		var isEdge = userAgent.indexOf("Edge") > -1 && !isIE; //判断是否IE的Edge浏览器  

		if(!isIE && !isEdge && !isIE11) { //兼容chrome和firefox  
			var _beforeUnload_time = 0,
				_gap_time = 0;
			var is_fireFox = navigator.userAgent.indexOf("Firefox") > -1; //是否是火狐浏览器  
			window.onunload = function() {
				_gap_time = new Date().getTime() - _beforeUnload_time;
				if(_gap_time <= 5) {
					vw_common.ConLog(_uName, "exitFn|" + _fnName);
					vw_common.ConLog(_uName, "logout"); //浏览器关闭
				} else { //浏览器刷新  
				}
			}
			window.onbeforeunload = function() {
				_beforeUnload_time = new Date().getTime();
				if(is_fireFox) { //火狐关闭执行  
					vw_common.ConLog(_uName, "exitFn|" + _fnName);
					vw_common.ConLog(_uName, "logout"); //浏览器关闭
				}
			}
		}
	}

	function createHeader() {
		var name = _uName;
		if(name.length > 7) {
			name = name.substr(0, 6) + "..."; //如果用户名长度大于7，截取前6个字
		}

		var pagename = window.location.pathname.split('/');
		var flag = pagename[pagename.length - 1] != VW_INTERFACE.PAGES.MAINFORM && _fid != 0;

		var html = [
			"<div id='vw-navbar' class='navbar navbar-default'>",
			"	<div class='navbar-header'>",
			"		<a id='vw-navbar-brand' class='navbar-brand'>",
			"			<img class='vw-logo' src='image/logo.png'>",
			"			<div class='title'>" + VW_INTERFACE.CAPTION + "</div>",
			VW_INTERFACE.CAPTION_EN ? "<div class='title_en'>" + VW_INTERFACE.CAPTION_EN + "</div>" : "",
			"		</a>",
			"	</div>",
			"	<div class='collapse navbar-collapse'>",
			"		<p id='vw-navbar-text' class='navbar-text'>" + _fnName + "</p>",
			"		<div id='vw-navbar-custom'></div>",
			"		<ul class='nav navbar-nav navbar-right'>",
			"			<li class='dropdown'>",
			"  				<a href='#' id='vw-navbar-msg' class='dropdown-toggle' data-toggle='dropdown'>",
			"					<span id='vw-icon-envelope' class='fa fa-envelope' aria-hidden='true'></span>",
			"					<span id='msg_c_0' class='badge vw-badge'>-1</span>", //此处标记更新消息数ID
			"				</a>",
			"  				<ul class='dropdown-menu extended inbox'>",
			"					<li><p>您有 <span id='msg_c_1'>-1</span> 条新消息</p></li>", //此处标记更新消息数ID
			"					<li id='msg_0'></li>",
			"					<li id='msg_1'></li>",
			"					<li id='msg_2'></li>",
			"					<li><a id='msg_all' href='#'>查看全部消息<i class='fa fa-arrow-circle-o-right' style='float:right;padding-top: 4px;'></i></a></li>",
			"  				</ul>",
			"			</li>",
			"			<li class='dropdown user' data-toggle='tooltip' data-placement='left' title='你好，" + _uName + "!'>",
			"				<a href='#' id='username' class='dropdown-toggle' data-toggle='dropdown'>",
			"					<img alt='' src='image/avatar.png' alt='欢迎：' width='32' height='32'>",
			"					<span class='username'>" + name + "</span>",
			"					<i class='icon-angle-down'></i>",
			"				</a>",
			"				<ul class='dropdown-menu'>",
			"					<li id='_userinfo'><a href='#'><i class='fa fa-user'></i>&nbsp;&nbsp;用户信息</a></li>",
			"					<li id='_inbox'><a id='msg_c_2'><i class='fa fa-envelope'></i>&nbsp;&nbsp;站内信(-1)</a></li>", //此处标记更新消息数ID
			"					<li class='divider'></li>",
			"					<li id='_changepwd'><a href='#'><i class='fa fa-key'></i>&nbsp;&nbsp;修改密码</a></li>",
			"					<li class='divider'></li>",
			"					<li id='_exit'><a href='#'><i class='fa fa-sign-out'></i>&nbsp;&nbsp;退出登录</a></li>",
			"				</ul>",
			"			</li>",
			flag ? "<li id='_backtomain'><a id='Homepage' href='#'><i id='home' class='glyphicon glyphicon-log-out'></i></a></li>" : "",
			"		</ul>",
			"	</div>",
			"</div>"
		].join('');
		$header.html(html);
		$footer.html('<div class="navbar navbar-default navbar-fixed-bottom vw-footer"></div>');
	};

	//前三条未读消息，显示详情，并在读后刷新header
	function ShowOneMsg(index) {
		var html = "<ul class='list-group'>";
		html += "<li class='list-group-item list-group-item-info'><strong>主题：</strong>" + msgcontents[index].text + "</li>";
		html += "<li class='list-group-item'><strong>发送人：</strong>" + msgcontents[index].user + "</li>";
		html += "<li class='list-group-item'><strong>发送时间：</strong>" + msgcontents[index].time + "</li>";
		if(msgcontents[index].stat == "审核通过") {
			html += "<li class='list-group-item'><strong>审核状态：</strong><span class='label label-success'>" + msgcontents[index].stat + "</li>";
		} else if(msgcontents[index].stat == "审核未通过") {
			html += "<li class='list-group-item'><strong>审核状态：</strong><span class='label label-danger'>" + msgcontents[index].stat + "</li>";
		} else {
			html += "<li class='list-group-item'><strong>审核状态：</strong><span class='label label-info'>" + msgcontents[index].stat + "</li>";
		}
		html += "<li class='list-group-item'><strong>详情：</strong>" + msgcontents[index].content + "</li>";
		html += "</ul>";

		bDialog.open({
			title: '查看消息',
			dom: html,
			dialogMaxButton: false,
			customClass: 'blue',
			width: 500,
			height: 315,
			onHidden: refreshMsg,
		});

		vw_common.customAjax({
			url: VW_INTERFACE.WEBSERVICE.LoginManage("CheckNotes"),
			type: "POST",
			dataType: "json",
			data: {
				"id": msgcontents[index].id
			}
		});
	};

	//调用接口读取未读消息数量和内容信息
	function refreshMsg() {

		vw_common.customAjax({
			url: VW_INTERFACE.WEBSERVICE.LoginManage("GetUnReadCount"),
			type: "POST",
			dataType: "json",
			data: {
				"userID": _uid,
			}
		}, function(e) {
			$("#msg_c_0").text(e.count);
			$("#msg_c_1").text(e.count);
			$("#msg_c_2").html("<i class='fa fa-envelope'></i>&nbsp;&nbsp;站内消息（" + e.count + "）");
		});

		vw_common.customAjax({
			url: VW_INTERFACE.WEBSERVICE.LoginManage("GetNotes"),
			type: "POST",
			dataType: "json",
			data: {
				"userID": _uid,
				"status": 0, //-1:全部 0:未读 1:已读
				"start": 0, //页码
				"limit": 3, //条数
			}
		}, function(e) {
			//清除原有内容
			$("#msg_0").empty();
			$("#msg_1").empty();
			$("#msg_2").empty();
			//向页面填充未读站内信内容
			if(e.notes != undefined) {
				for(var i = 0; i < e.notes.data.length; i++) {
					var msgHtml = "";
					msgHtml += "	<a href='#'>";
					msgHtml += "		<span class='photo'><img style='vertical-align: top;' src='image/avatar.png'/></span>";
					msgHtml += "		<span class='subject'>";
					var stat = e.notes.data[i][2].value;
					if(stat == "审核通过") {
						msgHtml += "<span class='from'><span class='label label-success'>" + stat + "</span>";
					} else if(stat == "审核未通过") {
						msgHtml += "<span class='from'><span class='label label-danger'>" + stat + "</span>";
					} else {
						msgHtml += "<span class='from'><span class='label label-info'>" + stat + "</span>";
					}
					msgHtml += "			<small>（来自:" + e.notes.data[i][3].value + "）</small><br/>";
					msgHtml += "			<span class='time'><small>" + e.notes.data[i][4].value + "</small></span><br/>";
					msgHtml += "		</span></span>";
					msgHtml += "		<span class='message'><small>" + e.notes.data[i][1].value + "</small></span>";
					msgHtml += "</a>";
					$("#msg_" + i).append(msgHtml);

					//站内信详情
					var msgcontent = {};
					msgcontent.text = e.notes.data[i][1].value;
					msgcontent.stat = e.notes.data[i][2].value;
					msgcontent.time = e.notes.data[i][4].value;
					msgcontent.user = e.notes.data[i][3].value;
					msgcontent.content = e.notes.content[i];
					msgcontent.id = e.notes.id[i];
					msgcontents[i] = msgcontent;
				}
			}
		});
	};

	return {
		init: Init
	};

});